<?php
class Enterprisesmodel extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	public function init()
	{
		$dbHandle = $this->load->database('ckhandle', TRUE);
		if($dbHandle == ''){
			error_log('can not create db handle','qna');
			echo (print_r($dbHandle,true));
		}
		return $dbHandle;
	}

public function getStoreCoupons($storeId){
		$dbHandle = $this->init();
		$date=date('Y-m-d');
		$query = "select cpn.*, strCpn.*, str.* from deals_deal_tbl cpn, deals_store_tbl str, deals_store_deals_tbl strCpn where cpn.deals_id=strCpn.deals_id and strCpn.store_id=str.store_id and strCpn.store_id='$storeId' and cpn.deals_expiredate >=  '$date'";
		error_log("get store coupons query is ".$query);
		$result = mysql_query($query);
		$couponsArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponsArray[$i] = $row;
			$i++;
		}
		return $couponsArray;
	}

	public function getAlphaStores($alpha){
		$dbHandle = $this->init();
		$date=date('Y-m-d');
		if($alpha=='0-9'){
			$query = "select temp.* from (SELECT s.*, COUNT( * ) AS noOfCoupons
			FROM deals_deal_tbl c, deals_store_tbl s, deals_store_deals_tbl sc
			WHERE c.deals_id = sc.deals_id
			AND s.store_id = sc.store_id
			AND c.deals_expiredate >=  '$date'
			AND c.deals_status='Yes'
			AND (LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '0' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '1' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '2' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '3' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '4' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '5' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '6' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '7' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '8' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '9')
			GROUP BY s.store_id union
			SELECT s.*, 0 AS noOfCoupons
			FROM deals_store_tbl s, deals_store_deals_tbl sc
			WHERE s.store_id != sc.store_id
			AND (LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '0' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '1' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '2' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '3' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '4' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '5' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '6' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '7' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '8' OR LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '9')
			GROUP BY s.store_id) as temp
			group by temp.store_id ORDER BY temp.store_order ASC";
		} elseif($alpha!=''){
			$query = "select temp.* from (SELECT s.*, COUNT( * ) AS noOfCoupons
FROM deals_deal_tbl c, deals_store_tbl s, deals_store_deals_tbl sc
WHERE c.deals_id = sc.deals_id
AND s.store_id = sc.store_id
AND c.deals_expiredate >=  '$date'
AND c.deals_status='Yes'
AND (LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '$alpha')
GROUP BY s.store_id union
SELECT s.*, 0 AS noOfCoupons
FROM deals_store_tbl s, deals_store_deals_tbl sc
WHERE s.store_id != sc.store_id
AND (LEFT(SUBSTRING_INDEX(s.store_name, ' ', 1), 1) = '$alpha')
GROUP BY s.store_id) as temp where temp.store_status='Yes'
group by temp.store_id ORDER BY temp.store_order ASC";
		} else {
			$query = "select temp.* from (SELECT s.*, COUNT( * ) AS noOfCoupons
FROM deals_deal_tbl c, deals_store_tbl s, deals_store_deals_tbl sc
WHERE c.deals_id = sc.deals_id
AND s.store_id = sc.store_id
AND c.deals_expiredate >=  '$date'
AND c.deals_status='Yes'
GROUP BY s.store_id union
SELECT s.*, 0 AS noOfCoupons
FROM deals_store_tbl s, deals_store_deals_tbl sc
WHERE s.store_id != sc.store_id
GROUP BY s.store_id) as temp where temp.store_status='Yes'
group by temp.store_id ORDER BY temp.store_order ASC";
		}
		error_log("Query is ".$query);
		$result = mysql_query($query);
		$alphaStoresArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$alphaStoresArray[$i] = $row;
			$i++;
		}
		return $alphaStoresArray;
	}

	function updateStoreViews($storeId, $month, $year){
		$dbHandle = $this->init();
		$query = "SELECT * FROM ck_storeviews_tbl WHERE storeviews_store_id = '$storeId' and views_month='$month' and views_year='$year'";
		error_log('qury234 is--'.$query);
		$result = mysql_query ($query);
		if(mysql_num_rows ($result) >= 1)
		{
			$update = "UPDATE ck_storeviews_tbl SET store_views = (store_views + 1) WHERE storeviews_store_id = '$storeId' and views_month='$month' and views_year='$year'";
			error_log('qury345 is--'.$update);
			$result = mysql_query ($update);
		} else {
			$insert = "INSERT INTO ck_storeviews_tbl(storeviews_store_id, store_views, views_month, views_year) VALUES ('".$storeId."', '1', '$month', '$year')";
			error_log('qury456 is--'.$insert);
			$result = mysql_query ($insert);
		}
		$update = "UPDATE ck_storeviews_tbl SET `store_total_views` = (SELECT SUM(x.`store_views`) FROM (SELECT * FROM storeviews_tbl) AS x WHERE x.`storeviews_store_id` =  storeviews_tbl.`storeviews_store_id`) where storeviews_store_id='$storeId'";
		error_log('qury567 is--'.$update);
		$result = mysql_query ($update);
		return true;
	}

	function getMonthlyStoreViews($startMonth, $startYear, $endMonth, $endYear){
		$dbHandle = $this->init();
		//$query = "SELECT S.*, S.store_image, SV.store_views, SV.store_total_views FROM ck_storeviews_tbl SV, deals_store_tbl S WHERE  SV.storeviews_store_id = S.store_id and SV.views_month>='$startMonth' and SV.views_year>='$startYear' and SV.views_month<='$endMonth' and SV.views_year<='$endYear' ORDER BY SV.store_total_views DESC";
		$query = "SELECT S.*, sum(SV.store_views) AS store_views, SV.store_total_views FROM ck_storeviews_tbl SV, deals_store_tbl S WHERE SV.storeviews_store_id = S.store_id and SV.views_month>='$startMonth' and SV.views_year>='$startYear' and SV.views_month<='$endMonth' and SV.views_year<='$endYear' group by S.store_id ORDER BY SV.store_total_views DESC";
		//error_log("query is ".$query);
		$result = mysql_query($query);
		//error_log("result is ".print_r($result, true));
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		//error_log("*****".print_r($resultArray, true));
		return $resultArray;
	}

	//-----------LOGIN & LOGOUT----------------------------------------------

	function uploadProductPhotoCode($filename, $filetemp){
		$uploadfile = '';
		if(isset($filetemp) && !empty($filetemp)) {
			$baseName = basename($filename);
			$uploadfile = VIEWSTOREIMG . $baseName;
			error_log($filetemp.">>>>>>>>>>>>>".$filename);
			move_uploaded_file($filetemp, $uploadfile);
		}
	 return;
	}

	public function addCategory($post){
		$dbHandle = $this->init();
		$query = "insert into deals_category_tbl(category_name, category_url,category_status, category_description, metatitle, metakeyword, metadescription) values('".addslashes($post['category_name'])."','".addslashes($post['category_url'])."','".addslashes($post['category_status'])."','".addslashes($post['category_description'])."', '".addslashes($post['metatitle'])."', '".addslashes($post['metakeyword'])."', '".addslashes($post['metadesc'])."') ";
		$result = $dbHandle->query($query);
	}
	
	public function getAllCategories(){
		$dbHandle = $this->init();
		$query = "select * from deals_category_tbl order by category_name asc";
		$result = mysql_query($query);
		$categoriesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$categoriesArray[$i] = $row;
			$i++;
		}
		return $categoriesArray;
	}

	public function updateCategory($post){
		$dbHandle = $this->init();
		$query = "UPDATE deals_category_tbl SET category_name='".addslashes($post['category_name'])."', category_url='".addslashes($post['category_url'])."', category_status='".addslashes($post['category_status'])."', category_description='".addslashes($post['category_description'])."', metatitle='".addslashes($post['metatitle'])."',metakeyword='".addslashes($post['metakeyword'])."',metadescription='".addslashes($post['metadesc'])."' WHERE category_id='".addslashes($post['category_id'])."'";
		$result = $dbHandle->query($query);
	}


	public function getCategoryDetails($category_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM deals_category_tbl where category_id=$category_id";
		$result = mysql_query($query);
		$categoryArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$categoryArray[$i] = $row;
			$i++;
		}
		return $categoryArray;
	}

	public function getStoreCategories($store_id){
		$dbHandle = $this->init();
		$query = "SELECT ck_cs.category_id FROM deals_category_store_tbl ck_cs, deals_category_tbl ck_c where ck_cs.category_id=ck_c.category_id and ck_cs.store_id='$store_id'";
		$result = mysql_query($query);
		$storeCategoriesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			array_push($storeCategoriesArray, $row['category_id']);
			error_log("storeCtaegoriesArray is ".print_r($storeCategoriesArray, true));
			$i++;
		}
		return $storeCategoriesArray;
	}


	public function getStoreBanners($store_id){
		$dbHandle = $this->init();
		$query = "select * from ck_banners_tbl where banner_store_id='$store_id' order by rand() limit 1";
		$result = mysql_query($query);
		$bannersArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$bannersArray[$i] = $row;
			$i++;
		}
		return $bannersArray;
	}

	public function getStoreDetails($store_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM deals_store_tbl where store_id=$store_id";
		$result = mysql_query($query);
		$storesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$storesArray[$i] = $row;
			$i++;
		}
		return $storesArray;
	}

	public function authenticate($post){
		$dbHandle = $this->init();
		$username=$post['username'];
		$password=$post['password'];
		$query = "SELECT * FROM deals_user_tbl where user_username='$username' and user_password='$password' and user_status='Yes'";
		$result = mysql_query($query);
		$userArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$userArray[$i] = $row;
			$i++;
		}
		return $userArray;
	}

	//------------------USERS-------------------------------------------

	public function getAllUsers(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM deals_user_tbl where user_status='Yes'";
		$result = mysql_query($query);
		$usersArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$usersArray[$i] = $row;
			$i++;
		}
		//error_log(print_r($usersArray));
		return $usersArray;
	}

	public function insertUser($post){
		$dbHandle = $this->init();
		$query = "insert into deals_user_tbl(user_username, user_password, user_status) values('".$post['username']."', '".$post['password']."', 'Yes')";
		//error_log("insert user ".$query);
		$result = $dbHandle->query($query);
	}

	public function deleteUser($userId){
		$dbHandle = $this->init();
		$query = "update deals_user_tbl set user_status='No' where user_id='$userId'";
		$result = $dbHandle->query($query);
	}

	public function addStore($post){
		$dbHandle = $this->init();
		if(isset($post['files']['store_image']['name']) && $post['files']['store_image']['name']!='' && $post['files']['store_image']['tmp_name']!=''){
			$pht = time()."_".$post['files']['store_image']['name'];
			error_log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddinsert user ".print_r($pht));
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['store_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		if(isset($post['files']['store_banner']['name']) && $post['files']['store_banner']['name']!='' && $post['files']['store_banner']['tmp_name']!=''){
			$pht = time()."_".$post['files']['store_banner']['name'];
			$filename1 = "/public/images/".$pht;
			$filetemp1 = $post['files']['store_banner']['tmp_name'];
			$this->uploadProductPhotoCode($filename1, $filetemp1);
		} else {
			$filename1 = "";
		}
		$query = "insert into deals_store_tbl(store_cat_id, store_name, store_status, store_type, store_image, store_banner, store_url, store_affilateurl, store_redirecturl, store_utmsource, store_description, metatitle, metakeyword, metadescription) values('".addslashes($post['relevant_category_id'])."' , '".addslashes($post['store_name'])."', '".addslashes($post['store_status'])."', '".addslashes($post['store_type'])."', '".$filename."', '".$filename1."', '".addslashes($post['store_url'])."', '".addslashes($post['store_affilateurl'])."', '".addslashes($post['store_redirecturl'])."', '".addslashes($post['store_utmsource'])."','".addslashes($post['store_description'])."','".addslashes($post['metatitle'])."','".addslashes($post['metakeyword'])."','".addslashes($post['metadescription'])."')";
		error_log(" add store **** model ".$query);
		$result = $dbHandle->query($query);
		$store_id=$dbHandle->insert_id();
		foreach($post['store_category_tbl_id'] as $temp){
			$query="insert into deals_category_store_tbl(category_id, store_id) values('$temp', $store_id)";
			error_log(" add store ****<<<<<<<<<<<<<<<<<<<<<<<<<<<<< model ".$query);
			$result = $dbHandle->query($query);
		}
		return $store_id;
	}
	
	



	public function updateStore($post){
		error_log("update store ?????????????????????? model ".print_r($post, true));
		$dbHandle = $this->init();
		if ($post['files']['store_image']['name']!=''){
			$pht = $post['files']['store_image']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['store_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		}else {
			$filename=$post['store_old_img'];
		}
		if ($post['files']['store_banner']['name']!=''){
			$pht = $post['files']['store_banner']['name'];
			$filename1 = "/public/images/".$pht;
			$filetemp1 = $post['files']['store_banner']['tmp_name'];
			$this->uploadProductPhotoCode($filename1, $filetemp1);
		}else {
			$filename1=$post['store_old_img1'];
		}
		error_log("update store *****, model");
			$query = "UPDATE deals_store_tbl SET store_cat_id='".addslashes($post['relevant_category_id'])."', store_name='".addslashes($post['store_name'])."', store_status='".addslashes($post['store_status'])."', store_image='".$filename."', store_url='".addslashes($post['store_url'])."', store_description='".addslashes($post['store_description'])."',metatitle='".addslashes($post['metatitle'])."', metakeyword='".addslashes($post['metakeyword'])."', metadescription='".addslashes($post['metadescription'])."', store_affilateurl='".addslashes($post['store_affilateurl'])."' WHERE store_id='".addslashes($post['store_id'])."'";
		$result = $dbHandle->query($query);
		$query = "delete from deals_category_store_tbl where store_id='".$post['store_id']."'";
		$result = $dbHandle->query($query);
		
	foreach($post['store_category_tbl_id'] as $temp){
			$query="insert into deals_category_store_tbl(category_id, store_id) values('$temp', '".$post['store_id']."')";
			error_log(" add store **** model ".$query);
			$result = $dbHandle->query($query);
		} 

	}

	public function updatePermissions($post){
		$dbHandle = $this->init();
		//error_log("post arrif(!isset($perms)) {
		$perms=$post['permission'];
		$perms=implode(",", $perms);
		//error_log("permissions ".print_r($perms, true));
		$query = "update deals_user_tbl set user_permissions='$perms' where user_id='".$post['username']."'";
		//error_log("query ".$query);
		$result = $dbHandle->query($query);
	}


	public function getAllBanners(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM ck_banners_tbl where banner_store_id!=0 ORDER BY banner_alt";
		$result = mysql_query($query);
		$bannerArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$bannerArray[$i] = $row;
			$i++;
		}
		return $bannerArray;
	}


	public function addBanner($post){
		error_log("add code **** model ".print_r($post,true));
		$dbHandle = $this->init();
		if(isset($post['files']['banner_image_path']['name']) && $post['files']['banner_image_path']['name']!='' && $post['files']['banner_image_path']['tmp_name']!=''){
			$pht = $post['files']['banner_image_path']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['banner_image_path']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		if(!empty($post['banner_image'])){
			$filename = $post['banner_image'];
		}
		$query = "insert into ck_banners_tbl(banner_alt, banner_image, banner_link, banner_store_id, banner_status) values('".addslashes($post['banner_alt'])."' , '".addslashes($filename)."' , '".addslashes($post['banner_link'])."' ,  '".implode(",", $post['banner_store_id'])."', '".addslashes($post['banner_status'])."')";
		error_log("nitesh---------------------bat".$query);
		$result = $dbHandle->query($query);
	}

	public function addTags($post){
		$dbHandle=$this->init();
		$query = "insert into ck_tags_tbl(store_id, store_tag) values
		('".addslashes($post['store_id'])."', '".addslashes($post['store_url'])."')";
		$result = $dbHandle->query($query);
	}

	//-------------------------Show and Edit Coupons-------------------------------------------

	public function addCoupon($post){
		//error_log("add code XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX**** model ".print_r($post,true));

		$dbHandle = $this->init();
		$dlink=$post['coupon_affilateurl'];
		
		if(isset($post['files']['coupon_banner']['name']) && $post['files']['coupon_banner']['name']!='' && $post['files']['coupon_banner']['tmp_name']!=''){
			$pht = $post['files']['coupon_banner']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['coupon_banner']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
	
		$query = "insert into deals_deal_tbl(deals_userid, deals_name, deals_code, deals_saving, deals_price, deals_type,  deals_description, deals_affilateurl, deals_startdate, deals_expiredate, deals_status, deals_category) values
		('".addslashes($post['user_id'])."' , '".addslashes($post['coupon_name'])."' , '".addslashes($post['coupon_code'])."' , '".addslashes($post['coupon_saving'])."' , '".addslashes($post['coupon_price'])."' , '".addslashes($post['coupon_type'])."' , '".addslashes($post['coupon_description'])."' , '".addslashes($dlink)."', '".addslashes($post['coupon_startdate'])."' , '".addslashes($post['coupon_expiredate'])."' , '".addslashes($post['coupon_status'])."' , '".addslashes($post['coupon_cat'])."' )";
		error_log("nitesh-???????????????????--------------------bat".$query);
		$result = $dbHandle->query($query);
		$deals_id=$dbHandle->insert_id();
		foreach($post['code_store'] as $temp){
			$query="insert into deals_store_deals_tbl(store_id, deals_id) values('$temp', $deals_id)";
			//error_log(" add coupon *???????????????????????????????*** model ".$query);
			$result = $dbHandle->query($query);
		}
	}
	
	public function detailpage($post){
		error_log("add code ********************************** model ".print_r($post,true));
		$dbHandle = $this->init();
		if(isset($post['deals_url']) && $post['deals_url']!=''){
			$cpnurl = $post['deals_url'];
		}else{
			$cpnurl = '';
		}
		if(isset($post['coupon_description_2']) && $post['coupon_description_2']!=''){
			$desc = $post['coupon_description_2'];
		}else{
			$desc = '';
		}
		if(isset($post['terms_and_conditions']) && $post['terms_and_conditions']!=''){
			$tandd = $post['terms_and_conditions'];
		}else{
			$tandd = '';
		}
		
		$dlink = $dlink=$post['coupon_affilateurl'];
	
		if(isset($post['files']['coupon_banner']['name']) && $post['files']['coupon_banner']['name']!='' && $post['files']['coupon_banner']['tmp_name']!=''){
			$pht = $post['files']['coupon_banner']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['coupon_banner']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
	
		$query = "insert into deals_deal_tbl(deals_userid, deals_name, coupon_code, coupon_type, coupon_bank, coupon_description, coupon_affilateurl, coupon_startdate, deals_expiredate, deals_status, coupon_category, coupon_occasion, deals_url, coupon_description2, coupon_termsandconditions, coupon_banner) values
		('".addslashes($post['user_id'])."' , '".addslashes($post['deals_name'])."' , '".addslashes($post['coupon_code'])."' , '".addslashes($post['coupon_type'])."' , '".addslashes($post['coupon_bank'])."' , '".addslashes($post['coupon_description'])."' , '".addslashes($dlink)."', '".addslashes($post['coupon_startdate'])."' , '".addslashes($post['deals_expiredate'])."' , '".addslashes($post['deals_status'])."' , '".addslashes($post['coupon_cat'])."' , '".addslashes($post['coupon_occasion'])."' , '".addslashes($cpnurl)."' , '".addslashes($desc)."' , '".addslashes($tandd)."', '".addslashes($filename)."')";
		error_log("nitesh---------------------bat".$query);
		$result = $dbHandle->query($query);
		$deals_id=$dbHandle->insert_id();
		foreach($post['code_store'] as $temp){
			$query="insert into deals_store_deals_tbl(store_id, deals_id) values('$temp', $deals_id)";
			error_log(" add coupon **** model ".$query);
			$result = $dbHandle->query($query);
		}
	}

	public function updateCoupon($post){
		error_log("add code **** model ".print_r($post,true));
		//$date=$post['deals_expiredate'];
		//$post['deals_expiredate']=substr($date, -4)."-".substr($date, 0, strlen($date)-5);
		$dbHandle = $this->init();
		if(isset($post['deals_url']) && $post['deals_url']!=''){
			$cpnurl = $post['deals_url'];
		}else{
			$cpnurl = '';
		}
		if(isset($post['coupon_description_2']) && $post['coupon_description_2']!=''){
			$desc = $post['coupon_description_2'];
		}else{
			$desc = '';
		}
		if(isset($post['terms_and_conditions']) && $post['terms_and_conditions']!=''){
			$tandd = $post['terms_and_conditions'];
		}else{
			$tandd = '';
		}
	
		$dlink = $dlink=$post['coupon_affilateurl'];
		if(isset($post['files']['coupon_banner']['name']) && $post['files']['coupon_banner']['name']!='' && $post['files']['coupon_banner']['tmp_name']!=''){
			$pht = $post['files']['coupon_banner']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['coupon_banner']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = $post['coupon_banner1'];
		}
		$query = "UPDATE deals_deal_tbl SET deals_userid='".addslashes($post['user_id'])."', deals_name='".addslashes($post['deals_name'])."', coupon_code='".addslashes($post['coupon_code'])."', coupon_saving='".addslashes($post['coupon_saving'])."', coupon_price='".addslashes($post['coupon_price'])."', coupon_type='".addslashes($post['coupon_type'])."', coupon_bank='".addslashes($post['coupon_bank'])."', coupon_description='".addslashes($post['coupon_description'])."', coupon_startdate='".addslashes($post['coupon_startdate'])."', deals_expiredate='".addslashes($post['deals_expiredate'])."', deals_status='".addslashes($post['deals_status'])."' , coupon_category='".addslashes($post['coupon_cat'])."' , coupon_occasion='".addslashes($post['coupon_occasion'])."' , coupon_description2='".addslashes($desc)."' , coupon_termsandconditions='".addslashes($tandd)."' , deals_url='".addslashes($cpnurl)."', coupon_banner='".$filename."', coupon_affilateurl='".$dlink."' WHERE deals_id='".addslashes($post['deals_id'])."'";
		$result = $dbHandle->query($query);
		$query ="delete from deals_store_deals_tbl where deals_id='".$post['deals_id']."'";
		$result = $dbHandle->query($query);
		foreach($post['code_store'] as $temp){
			$query="insert into deals_store_deals_tbl(store_id, deals_id) values('$temp', '".$post['deals_id']."')";
			$result = $dbHandle->query($query);
		}
	}


	public function updateBanner($post){
		error_log("add code **** model ".print_r($post,true));
		//$date=$post['deals_expiredate'];
		//$post['deals_expiredate']=substr($date, -4)."-".substr($date, 0, strlen($date)-5);
		$dbHandle = $this->init();
		if(isset($post['files']['banner_image_path']['name']) && $post['files']['banner_image_path']['name']!='' && $post['files']['banner_image_path']['tmp_name']!=''){
			$pht = $post['files']['banner_image_path']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['banner_image_path']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = $post['banner_image'];
		}
		$query = "UPDATE ck_banners_tbl SET banner_alt='".addslashes($post['banner_alt'])."', banner_image='".addslashes($filename)."', banner_link='".addslashes($post['banner_link'])."', banner_store_id='".implode(",", $post['banner_store_id'])."', banner_status='".addslashes($post['banner_status'])."' WHERE banner_id='".addslashes($post['banner_id'])."'";
		$result = $dbHandle->query($query);
	}


	public function getBannerDetails($banner_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM ck_banners_tbl where banner_id=$banner_id";
		$result = mysql_query($query);
		$bannerArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$bannerArray[$i] = $row;
			$i++;
		}
		return $bannerArray;
	}

	public function getCouponDetails($deals_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM deals_deal_tbl cc, deals_store_tbl cs, deals_store_deals_tbl csc where cc.deals_id='$deals_id' and cc.deals_id=csc.deals_id and cs.store_id=csc.store_id and cc.deals_status='Yes'";
		//error_log('checking coupons------- '.$query);
		$result = mysql_query($query);
		$couponArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponArray[$i] = $row;
			$i++;
		}
		return $couponArray;
	}

	//-------------------------Show and Edit comment-------------------------------------------

	public function getAllcomments(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM bat_comment_tbl where comment_status='New' order by comment_creationdate desc limit 25";
		$result = $dbHandle->query($query);
		$commentArray = array();
		foreach($result->result() as $row){
			array_push($commentArray, array(
					'comment_id'=>$row->comment_id,
					'comment_deals_id'=>$row->comment_deals_id,
					'comment_by'=>$row->comment_by,
					'comment_detail'=>$row->comment_detail,
					'comment_status'=>$row->comment_status,
					'comment_creationdate'=>$row->comment_creationdate,
			));
		}
		return $commentArray;
	}

	public function approveComments($comments_id){
		$dbHandle = $this->init();
		$query = "update bat_comment_tbl set comment_status='Yes' where comment_id=$comments_id";
		$result = $dbHandle->query($query);
	}

	public function rejectComments($comments_id){
		$dbHandle = $this->init();
		$query = "update bat_comment_tbl set comment_status='No' where comment_id=$comments_id";
		$result = $dbHandle->query($query);
	}

	public function cityWithoutDescription()
	{
		$dbHandle = $this->init();
		$query="Select * from bat_city_tbl where city_description='' AND city_status='Yes' order by city_displayname";
		$result = $dbHandle->query($query);
		$cityArray = array();
		foreach($result->result() as $row){
			array_push($cityArray, array(
					'city_id'=>$row->city_id,
					'city_displayname'=>$row->city_displayname,
			));
		}
		return $cityArray;
	}

	//-------------------------Show and Edit comment-------------------------------------------

	public function addVideo($post){
		$dbHandle = $this->init();
		$query = "insert into ck_videos_tbl (video_url, video_store_id) values('".addslashes($post['video_url'])."' , '".addslashes($post['video_store_id'])."')";
		$result = $dbHandle->query($query);
	}

	public function updateVideo($post){
		$dbHandle = $this->init();
		$query = "update ck_videos_tbl set video_url='".addslashes($post['video_url'])."' , video_store_id='".addslashes($post['video_store_id'])."' where videos_id='".$post['video_id']."'";
		$result = $dbHandle->query($query);
	}

	public function getStoreVideos($store_id){
		$dbHandle = $this->init();
		$query = "select * from ck_videos_tbl where video_store_id='$store_id' order by rand() limit 1";
		$result = mysql_query($query);
		$videoArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$videoArray[$i] = $row;
			$i++;
		}
		return $videoArray;
	}

	public function getVideoDetails($video_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM ck_videos_tbl where videos_id=$video_id";
		$result = mysql_query($query);
		$videoArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$videoArray[$i] = $row;
			$i++;
		}
		return $videoArray;
	}

	public function getSharedCoupons(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM deals_deal_tbl where deals_type='Shared' and deals_status='No'";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}

	//-------------------------Show and Edit occasions-------------------------------------------

	public function addOccasions($post){
		$dbHandle = $this->init();
		$query = "insert into ck_occasions_tbl (occasion_name, occasion_url, occasion_description, occasion_banner, occasion_bannerlink, occasion_status) values('".addslashes($post['occasion_name'])."' , '".addslashes($post['occasion_url'])."' , '".addslashes($post['occasion_description'])."' , '".addslashes($post['occasion_banner'])."' , '".addslashes($post['occasion_banner_link'])."' , '".addslashes($post['occasion_status'])."')";
		$result = $dbHandle->query($query);
	}

	public function updateOccasions($post){
		$dbHandle = $this->init();
		$query = "update ck_occasions_tbl set occasion_name='".addslashes($post['occasion_name'])."', occasion_url='".addslashes($post['occasion_url'])."', occasion_description='".addslashes($post['occasion_description'])."', occasion_banner='".addslashes($post['occasion_banner'])."', occasion_bannerlink='".addslashes($post['occasion_banner_link'])."', occasion_status='".addslashes($post['occasion_status'])."' where occasion_id='".addslashes($post['occasion_id'])."'";
		error_log('update query is>>>>>'.$query);
		$result = $dbHandle->query($query);
	}

	public function getAllOccasions(){
		$dbHandle = $this->init();
		$query = "select * from ck_occasions_tbl";
		$result = mysql_query($query);
		$resultArray = array();
		$i = 0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}

	public function getoccasionDetails($oc_id){
		$dbHandle = $this->init();
		$query = "select * from ck_occasions_tbl where occasion_id='$oc_id'";
		$result = mysql_query($query);
		$resultArray = array();
		$i = 0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	//-----------add n edit banks----------------//
	public function addBank($post){
		$dbHandle = $this->init();
		if(isset($post['files']['bank_image']['name']) && $post['files']['bank_image']['name']!='' && $post['files']['bank_image']['tmp_name']!=''){
			$pht = time()."_".$post['files']['bank_image']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['bank_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		$query = "insert into ck_banks_tbl (bank_name, bank_weburl, bank_url, bank_country, bank_status, bank_image) values('".addslashes($post['bank_name'])."', '".addslashes($post['bank_weburl'])."', '".addslashes($post['bank_url'])."', '".addslashes($post['bank_country'])."', '".addslashes($post['bank_status'])."', '".$filename."')";
		$result = $dbHandle->query($query);
	}
	 
	public function updateBanks($post){
		$dbHandle = $this->init();
		if(isset($post['files']['bank_image']['name']) && $post['files']['bank_image']['name']!='' && $post['files']['bank_image']['tmp_name']!=''){
			$pht = time()."_".$post['files']['bank_image']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['bank_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		$query = "update ck_banks_tbl set bank_name='".addslashes($post['bank_name'])."', bank_weburl='".addslashes($post['bank_weburl'])."', bank_url='".addslashes($post['bank_url'])."', bank_country='".addslashes($post['bank_country'])."', bank_status='".addslashes($post['bank_status'])."', bank_image='".$filename."' where bank_id='".addslashes($post['bank_id'])."'";
		error_log('update query is>>>>>'.$query);
		$result = $dbHandle->query($query);
	}
	
	public function getAllBanks(){
		$dbHandle = $this->init();
		$query = "select * from ck_banks_tbl group by bank_id order by bank_name asc";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	 
public function getStoreDetailsById($storeId){
		$dbHandle = $this->init();
		$query = "select * from deals_store_tbl cst, deals_category_store_tbl ccst where cst.store_id=ccst.store_id and cst.store_id='$storeId' and cst.store_status='Yes'";
		error_log("get store details query is ".$query);
		$result = mysql_query($query);
		$storeDetailsArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$storeDetailsArray[$i] = $row;
			$i++;
		}
		return $storeDetailsArray;
	}

	public function getBankDetails($b_id){
		$dbHandle = $this->init();
		$query = "select * from ck_banks_tbl where bank_id='$b_id'";
		$result = mysql_query($query);
		$resultArray = array();
		$i = 0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	

	public function getContactUs(){
		$dbHandle = $this->init();
		$query = "select * from ck_contactus_tbl order by contact_creationdate desc";
		$result = mysql_query($query);
		$resultArray = array();
		$i = 0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}

	public function getContactUsById($cId){
		$dbHandle = $this->init();
		$query = "select * from ck_contactus_tbl where contact_id='$cId'";
		$result = mysql_query($query);
		$resultArray = array();
		$i = 0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	
	public function storeredirectCheck($storeId){
		$dbHandle = $this->init();
		$query= "Select store_redirecturl from deals_store_tbl where store_id='$storeId'";
		$result = mysql_query($query);
		$storeRedirectDetailsArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$storeRedirectDetailsArray[$i] = $row;
			$i++;
		}
		return $storeRedirectDetailsArray;
	}
	
	//----- storedeal starts here-----------//
	
	public function getAllStores()
	{
		$dbHandle = $this->init();
		$query= "Select store_name, store_id from deals_store_tbl WHERE store_status = 'Yes' ORDER BY store_name ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function insertIntostoreDeals($post)
	{
		$dbHandle = $this->init();
		if(isset($post['files']['deal_banner']['name']) && $post['files']['deal_banner']['name']!='' && $post['files']['deal_banner']['tmp_name']!=''){
			$pht = time()."_".$post['files']['deal_banner']['name'];
			error_log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddinsert storedeal ".print_r($pht));
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['deal_banner']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		$store_id=$post['store_id'];
		$deal_url=$post['deal_url'];
		$deal_status=$post['deal_status'];
		$deal_description1=$post['deal_description1'];
		$deal_description2=$post['deal_description2'];
		$metatitle=$post['metatitle'];
		$metakeyword=$post['metakeyword'];
		$metadescription=$post['metadescription'];
		$insert="INSERT INTO `ck_storedeals_tbl` (`store_id` ,`store_url` ,`storedeals_desc1` ,`storedeals_desc2` ,`metatitle` ,`metakeyword` ,`metadescription` ,`storedeals_status`, `storedeals_banner`)VALUES ( '".addslashes($store_id)."', '".addslashes($deal_url)."','".addslashes($deal_description1)."','".addslashes($deal_description2)."','".addslashes($metatitle)."','".addslashes($metakeyword)."','".addslashes($metadescription)."','".addslashes($deal_status)."', '".addslashes($filename)."')";
		error_log('update query is>>>>>'.$insert);
		mysql_query($insert);
	}
	
	public function getStoreDeals()
	{
		$dbHandle = $this->init();
		$query= "Select sd.*, s.store_name from ck_storedeals_tbl sd, deals_store_tbl s WHERE sd.store_id=s.store_id order by sd.store_url";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	public function getStoreDetailsDeals($storedeals_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM ck_storedeals_tbl where storedeals_id=$storedeals_id";
		$result = mysql_query($query);
		$storesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$storesArray[$i] = $row;
			$i++;
		}
		return $storesArray;
	}
	public function updateStoreDeals($post)
	{
		$dbHandle = $this->init();
		if(isset($post['files']['deal_banner']['name']) && $post['files']['deal_banner']['name']!='' && $post['files']['deal_banner']['tmp_name']!=''){
				$pht = time()."_".$post['files']['deal_banner']['name'];
				error_log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddinsert storedeal ".print_r($pht));
				$filename = "/public/images/".$pht;
				$filetemp = $post['files']['deal_banner']['tmp_name'];
				$this->uploadProductPhotoCode($filename, $filetemp);
			} else{
				$filename = $post['old_deals_banner'];
			} 
		$storedeals_id=$post['storedeals_id'];
		$store_id=$post['store_id'];
		$deal_url=$post['deal_url'];
		$deal_status=$post['deal_status'];
		$deal_description1=$post['deal_description1'];
		$deal_description2=$post['deal_description2'];
		$metatitle=$post['metatitle'];
		$metakeyword=$post['metakeyword'];
		$metadescription=$post['metadescription'];
		$update="update ck_storedeals_tbl set store_id='".addslashes($store_id)."',store_url='".addslashes($deal_url)."',storedeals_desc1='".addslashes($deal_description1)."',storedeals_desc2='".addslashes($deal_description2)."',metatitle='".addslashes($metatitle)."',metakeyword='".addslashes($metakeyword)."',metadescription='".addslashes($metadescription)."',storedeals_status='".addslashes($deal_status)."',storedeals_banner='".addslashes($filename)."' where storedeals_id='$storedeals_id'";
		error_log('update query is ajay>>>>>'.$update);
		mysql_query($update);
	}
	
	public function getSharedCouponDetails($deals_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM deals_deal_tbl cc, deals_store_tbl cs, deals_store_deals_tbl csc where cc.deals_id='$deals_id' and cc.deals_id=csc.deals_id and cs.store_id=csc.store_id";
		//error_log('checking coupons------- '.$query);
		$result = mysql_query($query);
		$couponArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponArray[$i] = $row;
			$i++;
		}
		return $couponArray;
	}
	
	//----------popup offers starts-------------------//
	
	public function insertPopupOffers($post)
	{
		$dbHandle = $this->init();
		if(isset($post['files']['offer_image']['name']) && $post['files']['offer_image']['name']!='' && $post['files']['offer_image']['tmp_name']!=''){
			$pht = time()."_".$post['files']['offer_image']['name'];
			error_log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddinsert popupoffer ".print_r($pht));
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['offer_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		$insert="INSERT INTO `ck_popupoffer_tbl`(`offer_title`, `offer_image`, `offer_status`, `offer_alt`, `offer_url`) VALUES ('".addslashes($post['offer_title'])."', '".addslashes($filename)."', '".addslashes($post['offer_status'])."', '".addslashes($post['offer_alt'])."', '".addslashes($post['offer_url'])."')";
		error_log('update query is>>>>>'.$insert);
		mysql_query($insert);
	}
	
	public function getPopupOffer()
	{
		$dbHandle = $this->init();
		$query = "select * from ck_popupoffer_tbl order by offer_creationdate desc";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row=mysql_fetch_assoc($result))
		{
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getPopupOfferById($oId)
	{
		$dbHandle = $this->init();
		$query = "select * from ck_popupoffer_tbl where offer_id='$oId'";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row=mysql_fetch_assoc($result))
		{
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function updatePopupOffers($post)
	{
		$dbHandle = $this->init();
		if(isset($post['files']['offer_image']['name']) && $post['files']['offer_image']['name']!='' && $post['files']['offer_image']['tmp_name']!=''){
			$pht = time()."_".$post['files']['offer_image']['name'];
			error_log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddinsert popupoffer ".print_r($pht));
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['offer_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = $post['offer_old_img'];
		}
		$insert="UPDATE `ck_popupoffer_tbl` SET `offer_title`='".addslashes($post['offer_title'])."',`offer_image`='".addslashes($filename)."',`offer_status`='".addslashes($post['offer_status'])."', `offer_alt`='".addslashes($post['offer_alt'])."', `offer_url`='".addslashes($post['offer_url'])."' WHERE offer_id='".$post['offer_id']."'";
		error_log('update query is>>>>>'.$insert);
		mysql_query($insert);
	}
	
	//----------popup offers ends--------------------//
	
	public function getPopStores()
	{
		$dbHandle = $this->init();
		$query= "SELECT sum(cr.rating_star) as sum,count(cr.store_id) as count, cs.store_id, cs.store_name FROM ck_ratings_tbl cr, deals_store_tbl cs where cr.store_id=cs.store_id group by cr.store_id order by sum desc";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	//---------mailer-----------
	
	public function getMailerStores()
	{
		$dbHandle = $this->init();
		$query= "SELECT s.store_id,s.store_name, cs.subscribe_emailid, count(s.store_id) as count FROM deals_store_tbl s, ck_subscribe_tbl cs where s.store_id=cs.subscribe_store_id group by s.store_id ORDER BY `count` desc";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getMailerCoupons($post,$t, $start='0', $count='10')
	{
		$dbHandle = $this->init();
		if($t!='1'){
			$query= "SELECT c.*,s.store_id,s.store_name,s.store_url,s.store_image FROM deals_deal_tbl c, deals_store_tbl s, deals_store_deals_tbl cst WHERE c.deals_id=cst.deals_id and s.store_id=cst.store_id and s.store_id='".$post['mailer_store_id']."' and c.deals_expiredate>=curdate() and c.deals_status='Yes' and c.deals_type!='offer' order by c.coupon_creationdate desc limit $start, $count";
		}else{
			$query= "SELECT c.*,s.store_id,s.store_name,s.store_url,s.store_image FROM deals_deal_tbl c, deals_store_tbl s, deals_store_deals_tbl cst WHERE c.deals_id=cst.deals_id and s.store_id=cst.store_id and s.store_id='".$post['mailer_store_id']."' and c.deals_expiredate>=curdate() and c.deals_status='Yes' and c.deals_type='offer' order by c.coupon_creationdate desc limit $start, $count";
		}
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getMailList($sId)
	{
		$dbHandle = $this->init();
		$query= "SELECT * FROM ck_subscribe_tbl where subscribe_store_id='$sId' ORDER BY subscribe_creationdate desc";
		error_log('mail ................'.$query);
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}

}
